﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Core.Support
{
    public static class UserLibraryHelper
    {

        private static string _siteContentTypeName = string.Empty;

        public static string ContentTypeFieldName 
        {
            get
            {
                if (string.IsNullOrEmpty(_siteContentTypeName))
                    _siteContentTypeName = SPContext.Current.Web.Fields[SPBuiltInFieldId.ContentType].InternalName;
               return _siteContentTypeName ;
            }
        }

        public static string CurrentUserLibraryUrl 
        {
            get 
            {
                return SPContext.Current.Web.Url + "/" + SPContext.Current.Web.CurrentUser.LoginName.Split('\\')[1];
            } 
        }

        public static string CurrentUserLibraryName
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.LoginName.Split('\\')[1];
            }
        }

        public static SPDocumentLibrary GetUserLibrary(string loginName)
        {

            SPList userList = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        if (loginName.Contains('\\'))
                                loginName = loginName.Split('\\')[1];
                        userList = currentWeb.Lists.TryGetList(loginName);
                    }
                }
            });
            return userList as SPDocumentLibrary;
        }

        public static SPList GetLibrary(string libraryRelativeUrl)
        {
            SPList targetList = null;
            if (libraryRelativeUrl.Contains('\\'))
                libraryRelativeUrl = libraryRelativeUrl.Split('\\')[1];
            targetList = SPContext.Current.Web.GetList(string.Format("{0}/{1}", SPContext.Current.Web.Url, libraryRelativeUrl));
            //if (SPContext.Current.Web.Lists.TryGetList(loginName) == null)
            //    return null;
            //return SPContext.Current.Web.Lists[loginName];
            return targetList;
        }

        public static SPList GetUserList(string loginName)
        {
            SPList userList = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        if (loginName.Contains('\\'))
                            loginName = loginName.Split('\\')[1];
                        userList = currentWeb.Lists.TryGetList(loginName);
                    }
                }
            });
            return userList;
        }

        public static SPList GetUserList(string loginName, SPWeb currentWeb)
        {
            SPList userList = null;
            if (loginName.Contains('\\'))
                loginName = loginName.Split('\\')[1];
            userList = currentWeb.Lists.TryGetList(loginName);
            return userList;
        }


        public static SPList GetUserListForCurrentUser()
        {
            string userName = SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain();
            if (SPContext.Current.Web.Lists.TryGetList(userName) == null)
                return null;
            return SPContext.Current.Web.Lists[userName];
        }

        public static Guid CreateDocumentLibraryWithGroupPrivileges(string libraryName, SPWeb currentWeb, string groupName)
        {
            SPListTemplate listTemplate = currentWeb.ListTemplates[GlobalConstants.TEMPLATE_USER_LIBRARY];
            Guid newLibraryId = currentWeb.Lists.Add(libraryName, null, listTemplate);
            currentWeb.Lists[newLibraryId].BreakRoleInheritance(false);
            SPRoleDefinition oRole = currentWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
            SPRoleAssignment roleAssignment = new SPRoleAssignment(currentWeb.Groups[groupName]);
            roleAssignment.RoleDefinitionBindings.Add(oRole);
            currentWeb.Lists[newLibraryId].RoleAssignments.Add(roleAssignment);
            currentWeb.Update();
            return newLibraryId;
        }

        public static void MoveFile(int radicadoId, string sourceUser, string targetUser)
        { 
            
        }

    }
}
